// Filename: router.js
define([
    'jquery',
    'underscore',
    'backbone',
    ], function($, _, Backbone){
        var $html_loadding = '<div class="caculation-report" style="text-align:center"><img src="'+$template_admincp_url+'images/preloaders/blue/4.gif"/></div>';
        var AppRouter = Backbone.Router.extend({
            routes: {
                'admin': 'admin',
                'admin/*path': 'admin',
                'dashboard': 'dashboard',
                'dashboard/*path': 'dashboard',
                'article': 'article',
                'article/*path': 'article',
                'catalogue': 'catalogue',
                'catalogue/*path': 'catalogue',
                'level': 'level',
                'level/*path': 'level',
                'section': 'section',
                'section/*path': 'section',
                'setting': 'setting',
                'setting/*path': 'setting',
                'synce': 'synce',
                'synce/*path': 'synce',
                'translate': 'translate',
                'translate/*path': 'translate',
                'language': 'language',
                'language/*path': 'language',
                'gallery': 'gallery',
                'gallery/*path': 'gallery',
                'file_manager': 'fileManager',
                'file_manager/*path': 'fileManager',
                'catalogue_media': 'catalogueMedia',
                'catalogue_media/*path': 'catalogueMedia',
                'pages': 'pages',
                'pages/*path': 'pages',
                'catalogue_product': 'catalogue_product',
                'catalogue_product/*path': 'catalogue_product',
                'product': 'product',
                'product/*path': 'product',
                'case_product': 'case_product',
                'case_product/*path': 'case_product',
                'order': 'order',
                'order/*path': 'order',
                '*actions': 'defaultAction'

            },
            admin: function(){
                require(['views/admin'], function (adminView) {
                    adminView.render();
                });
            },
            dashboard: function(){
                require(['views/dashboard'], function (dashboardView) {
                    dashboardView.render();
                });
            },
            article: function(){
                require(['views/article'], function (articleView) {
                    articleView.render();
                });
            },
            article_static: function(){
                require(['views/article_static'], function (article_staticView) {
                    article_staticView.render();
                });
            },
            catalogue: function(){
                require(['views/catalogue'], function (catalogueView) {
                    catalogueView.render();
                });
            },
            level: function(){
                require(['views/level'], function (levelView) {
                    levelView.render();
                });
            },
            section: function(){
                require(['views/section'], function (sectionView) {
                    sectionView.render();
                });
            },
            setting: function(){
                require(['views/setting'], function (settingView) {
                    settingView.render();
                });
            },
            synce: function(){
                require(['views/synce'], function (synceView) {
                    synceView.render();
                });
            },
            translate: function(){
                require(['views/translate'], function (translateView) {
                    translateView.render();
                });
            },
            language: function(){
                require(['views/language'], function (languageView) {
                    languageView.render();
                });
            },
            gallery: function(){
                require(['views/gallery'], function (galleryView) {
                    galleryView.render();
                });
            },
            fileManager: function(){
                require(['views/fileManager'], function(fileManagerView){
                    fileManagerView.render();
                });
            },
            catalogueMedia: function(){
                require(['views/catalogueMedia'], function(catalogueMediaView){
                    catalogueMediaView.render();
                });
            },
            pages: function(){
                require(['views/pages'], function(pagesView){
                    pagesView.render();
                });
            },
            catalogue_product: function(){
                require(['views/catalogue_product'], function(cataProView){
                    cataProView.render();
                });
            },
            product: function(){
                require(['views/product'], function(productView){
                    productView.render();
                });
            },
            case_product: function(){
                require(['views/case_product'], function(caseProView){
                    caseProView.render();
                });
            },
            order: function(){
                require(['views/order'], function(orderView){
                    orderView.render();
                });
            },
            defaultAction: function(actions){
            // We have no matching route, lets display the home page
            }
        });
        var initialize = function(){
            var app_router = new AppRouter;
            Backbone.history.start({
                pushState: true,
                //root: $base_url.replace("http://localhost", "") + "admincp/"
				root: "/admincp/"
            });
            var url = location.href;
            $('.language').click(function(event){
                require(['views/language'], function(languageView){
                    languageView.changeLanguage(event);
                });
            });
            $("#clearCache").click(function(){
                showLoading('Clear Cache', $html_loadding);
                $(".messi").show(0, function(){
                    $.ajax({
                        url: $admincp_url + 'setting/clearCache',
                        async: false,
                        statusCode: {
                            404: function() {
                                $('.caculation-report').html('<div style="width:100%;">'+
                                    '<span style="float:left"><strong><i class="icon24 i-warning"></i>Warning:</strong></span>'+
                                    '<span class="badge" style="float:right; padding:5px">Could not contact server.</span>'+
                                '</div>');
                            },
                            500: function() {
                                $('.caculation-report').html('<div style="width:100%;">'+
                                    '<span style="float:left"><strong><i class="icon24 i-warning"></i>Warning:</strong></span>'+
                                    '<span class="badge" style="float:right; padding:5px">A server-side error has occurred.</span>'+
                                '</div>');
                            }
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            $('.caculation-report').html('<div style="width:100%;">'+
                                    '<span style="float:left"><strong><i class="icon24 i-warning"></i>Warning:</strong></span>'+
                                    '<span class="badge" style="float:right; padding:5px">'+textStatus+'</span>'+
                                '</div>');
                        },
                        success: function(data){
                            data = JSON.parse(data);
                            $('.caculation-report').html(data.html);
                        },
                    });
                });
            });
        };
        return {
            initialize: initialize
        };
    });